<?php
/**
 * 入驻管理
*
* @author karl <karl.tang@aiyuangong.com>
* @link http://www.aiyuangong.com
*/
namespace app\admin\controller\goods;

use app\admin\Controller;
use think\Exception;
use think\Db;

class Album extends Controller
{
    use\app\admin\traits\controller\Controller;

    protected static $blacklist = [
        'deleteforever',
        'clear'
    ];
    
    /**
     * 相册列表
     */
    public function index()
    {
        // 相册列表
        $keys = $this->request->param('keys/s','');
        $curpage = $this->request->param('curpage',1);
        $where = [];
        if($keys){
            $where['b.id|b.name'] = $keys;
        }
        $count = Db::name('album_class')->alias('a')->join('store b','a.store_id=b.id','LEFT')->where($where)->count();
        $album_class = Db::name('album_class')->alias('a')->join('store b','a.store_id=b.id','LEFT')
                ->field('a.*,b.name as store_name')
                ->where($where)->group('a.aclass_id')->limit(($curpage-1)*20,20)->select();
        $ids = array_keys(reset_by_key($album_class, 'aclass_id'));
        $pic_num = Db::name('album_pic')->where(['aclass_id' => ['in',$ids]])->field('aclass_id,count(apic_id) as pic_num')->group('aclass_id')->select();
        if(!empty($pic_num)){
            $pic_num = reset_by_key($pic_num, 'aclass_id');
        }
        $query = [
            'keys' => $keys,
            'curpage' => '[PAGE]'
        ];
        // 分页渲染内容
        $option = [
            'var_page' => 'curpage',
            'path' => url('admin/goods.album/index', $query)
        ];
        $page = \Page::make($album_class, 20, $curpage, $count, false, $option);
        $this->view->assign('pic_num', $pic_num);
        $this->view->assign('count', $count);
        $this->view->assign('list', $album_class);
        $this->view->assign('page', $page);
        return $this->view->fetch();
    }
    
    /**
     * 编辑相册
     */
    public function edit()
    {
        // 相片列表
        $class_id = $this->request->param('id/d',0);
        if($class_id){
            $where['aclass_id'] = $class_id;
        }
        $pic_list = Db::name('album_pic')->where($where)->paginate(40);
        $this->view->assign('list', $pic_list);
        return $this->view->fetch();
    }
    
    /**
     * 删除相册操作
     */
    public function deleteforever_class()
    {
        //删除相册
        $ids = $this->request->param('id');
        $where['aclass_id'] = ["in", $ids];
        $resource_class = Db::name('album_class')->where($where)->select();
        $resource_pic = Db::name('album_pic')->where($where)->select();
        //删除相册里的全部相片
        try{
            if (false === Db::name('album_class')->where($where)->delete()) {
                return ajax_return_adv_error($model->getError());
            }
            if (false === Db::name('album_pic')->where($where)->delete()) {
                return ajax_return_adv_error($model->getError());
            }
            if(!empty($resource_class)){
                foreach($resource_class as $key => $val){
                    if(file_exists(ROOT_PATH.'public/uploads/'.$val['aclass_cover'])){
                        @unlink(ROOT_PATH.'public/uploads/'.$val['aclass_cover']);
                    }
                }
            }
            if(!empty($resource_pic)){
                foreach($resource_pic as $key => $val){
                    if(file_exists(ROOT_PATH.'public/uploads/'.$val['apic_cover'])){
                        @unlink(ROOT_PATH.'public/uploads/'.$val['apic_cover']);
                    }
                }
            }
            return ajax_return_adv("删除成功");
        }catch(Exception $e){
            throw new Excetion($e->getMessage());
        }
    }
    
    /**
     * 删除图片操作
     */
    public function deleteforever_pic()
    {
        $ids = $this->request->param('id');
        $where['apic_id'] = ["in", $ids];
        $resource = Db::name('album_pic')->where($where)->select();
        if (false === Db::name('album_pic')->where($where)->delete()) {
            return ajax_return_adv_error($model->getError());
        }
        //删除资源
        if(!empty($resource)){
            foreach($resource as $key => $val){
                if(file_exists(ROOT_PATH.'public/uploads/'.$val['apic_cover'])){
                    @unlink(ROOT_PATH.'public/uploads/'.$val['apic_cover']);
                }
            }
        }
        return ajax_return_adv("删除成功");
    }
}